using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BCB.Model;
using BCB.BLL;
namespace BCB.BCBWebControl
{
    public class ProductShow : Repeater
    {
        private int CurrentPageNum;
        private int PageCount;
        private int CategoryId;
        private int _pageSize = 10;
        public int PageSize
        {
            get
            {
                return _pageSize;
            }
            set
            {
                _pageSize = value;
            }
        }
        public override ITemplate ItemTemplate
        {
            get
            {
                return base.ItemTemplate;
            }
            set
            {

                base.ItemTemplate = value;
            }
        }

        protected override void OnItemDataBound(RepeaterItemEventArgs e)
        {
            int itemIndex = e.Item.ItemIndex + 1;
            if ((e.Item.ItemType == ListItemType.AlternatingItem 
                || e.Item.ItemType == ListItemType.Item) && itemIndex % 4 == 0)
            {
                e.Item.Controls.Add(new LiteralControl("<br/>"));
            }
            base.OnItemDataBound(e);
        }
        protected override void Render(HtmlTextWriter writer)
        {
            base.Render(writer);
            int startPage = PreparePageNum();
            int pageAmount = PageCount - startPage >= 6 ? 6 : PageCount - startPage + 1;//the number of page should be dispalyed;
            writer.Write("<div class='pagenumber'>");
            for (int i = 0; i < pageAmount; i++)
            {
                if (i == 0 && startPage > 6)
                {
                    writer.Write(string.Format("<a href=?pagenum={0}&categoryId={1}> {2}</a>", startPage - 1, CategoryId, "<<"));
                }
                writer.Write(string.Format("<a href=?pagenum={0}&categoryId={1}> {2}</a>", startPage + i, CategoryId, startPage + i));
                writer.Write("  ");
                if (i == 5 && PageCount - startPage >= 6)
                {
                    writer.Write(string.Format("<a href=?pagenum={0}&categoryId={1}> {2}</a>", startPage + 6, CategoryId, ">>"));
                }
            }
            writer.Write("</div>");
        }
        /// <summary>
        /// Return the first page number according the current page number;
        /// </summary>
        /// <returns>the first page number</returns>
        private int PreparePageNum()
        {
            int rightCritalBound = PageCount % 6 == 0 ? 6 : PageCount % 6;//the right critical boundary
            int leftCritalBound = 6;//
            int pageSpace = CurrentPageNum % 6 == 0 ? 5 : CurrentPageNum % 6 - 1;//the space between currentpagenumber and firstpagenumber;
            int startPage;

            if (CurrentPageNum < leftCritalBound)// the left critical boundary;
            {
                startPage = 1;
            }
            else if (PageCount - CurrentPageNum < rightCritalBound)//the right critical boundary;
            {
                startPage = PageCount - rightCritalBound + 1;
            }
            else
            {
                startPage = CurrentPageNum - pageSpace;
            }
            return startPage;
        }

        protected override void OnLoad(EventArgs e)
        {
            if (Context.Request["pagenum"] != null)
            {
                CurrentPageNum = int.Parse(Context.Request["pagenum"].ToString());
            }
            else
            {
                CurrentPageNum = 1;
            }
            CategoryId = int.Parse(Context.Request["categoryId"].ToString());
            if (BLL.Products.GetProductNumsByCategoryId(CategoryId) / PageSize == 0)
            {
                PageCount = BLL.Products.GetProductNumsByCategoryId(CategoryId) / PageSize;
            }
            else
            {
                PageCount = BLL.Products.GetProductNumsByCategoryId(CategoryId) / PageSize + 1;
            }
            base.DataSource = BLL.Products.GetProductsByCategoryAndPageNum(CategoryId, CurrentPageNum, PageSize);
            base.DataBind();
        }
    }
}
